home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PCGPEV10.ZIP / TUT4.TXT < prev    next >
Text File  |  1994-05-05  |  16KB  |  409 lines

  1.  
  2.                    ╒═══════════════════════════════╕
  3.                    │         W E L C O M E         │
  4.                    │  To the VGA Trainer Program   │ │
  5.                    │              By               │ │
  6.                    │      DENTHOR of ASPHYXIA      │ │ │
  7.                    ╘═══════════════════════════════╛ │ │
  8.                      ────────────────────────────────┘ │
  9.                        ────────────────────────────────┘
  10.  
  11.                            --==[ PART 4 ]==--
  12.  
  13.  
  14.  
  15. ■ Introduction
  16.  
  17.  
  18. Howdy all! Welcome to the fourth part of this trainer series! It's a
  19. little late, but I am sure you will find that the wait was worth it,
  20. becase today I am going to show you how to use a very powerful tool :
  21. Virtual Screens.
  22.  
  23. If you would like to contact me, or the team, there are many ways you
  24. can do it : 1) Write a message to Grant Smith in private mail here on
  25.                   the Mailbox BBS.
  26.             2) Write a message here in the Programming conference here
  27.                   on the Mailbox (Preferred if you have a general
  28.                   programming query or problem others would benefit from)
  29.             3) Write to ASPHYXIA on the ASPHYXIA BBS.
  30.             4) Write to Denthor, Eze or Livewire on Connectix.
  31.             5) Write to :  Grant Smith
  32.                            P.O.Box 270 Kloof
  33.                            3640
  34.             6) Call me (Grant Smith) at 73 2129 (leave a message if you
  35.                   call during varsity)
  36.  
  37. NB : If you are a representative of a company or BBS, and want ASPHYXIA
  38.        to do you a demo, leave mail to me; we can discuss it.
  39. NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
  40.         quite lonely and want to meet/help out/exchange code with other demo
  41.         groups. What do you have to lose? Leave a message here and we can work
  42.         out how to transfer it. We really want to hear from you!
  43.  
  44.  
  45. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  46. ■  What is a Virtual Screen and why do we need it?
  47.  
  48. Let us say you are generating a complex screen numerous times on the fly
  49. (for example scrolling up the screen then redrawing all the sprites for
  50. each frame of a game you are writing.) Do you have any idea how awful it
  51. would look if the user could actually see you erasing and redrawing each
  52. sprite for each frame? Can you visualise the flicker effect this would
  53. give off? Do you realise that there would be a "sprite doubling" effect
  54. (where you see two copies of the same sprite next to each other)? In the
  55. sample program I have included a part where I do not use virtual screens
  56. to demonstrate these problems. Virtual screens are not the only way to
  57. solve these problems, but they are definately the easiest to code in.
  58.  
  59. A virtual screen is this : a section of memory set aside that is exactly
  60. like the VGA screen on which you do all your working, then "flip" it
  61. on to your true screen. In EGA 640x350x16 you automatically have a
  62. virtual page, and it is possible to have up to four on the MCGA using a
  63. particular tweaked mode, but for our puposes we will set one up using base
  64. memory.
  65.  
  66. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  67. ■  Setting up a virtual screen
  68.  
  69. As you will have seen in the first part of this trainer series, the MCGA
  70. screen is 64000 bytes big (320x200=64000). You may also have noticed that
  71. in TP 6.0 you arn't allowed too much space for normal variables. For
  72. example, saying :
  73.  
  74. VAR Virtual : Array [1..64000] of byte;
  75.  
  76. would be a no-no, as you wouldn't have any space for your other variables.
  77. What is the solution? I hear you enquiring minds cry. The answer : pointers!
  78. Pointers to not use up the base 64k allocated to you by TP 6.0, it gets
  79. space from somewhere else in the base 640k memory of your computer. Here is
  80. how you set them up :
  81.  
  82. Type Virtual = Array [1..64000] of byte;  { The size of our Virtual Screen }
  83.      VirtPtr = ^Virtual;                  { Pointer to the virtual screen }
  84.  
  85. VAR Virscr : VirtPtr;                      { Our first Virtual screen }
  86.     Vaddr  : word;                        { The segment of our virtual screen}
  87.  
  88. If you put this in a program as it stands, and try to acess VirScr, your
  89. machine will probably crash. Why? Because you have to get the memory for
  90. your pointers before you can acess them! You do that as follows :
  91.  
  92. Procedure SetUpVirtual;
  93. BEGIN
  94.   GetMem (VirScr,64000);
  95.   vaddr := seg (virscr^);
  96. END;
  97.  
  98. This procedure has got the memory for the screen, then set vaddr to the
  99. screens segment. DON'T EVER LEAVE THIS PROCEDURE OUT OF YOUR PROGRAM!
  100. If you leave it out, when you write to your virtual screen you will probably
  101. be writing over DOS or some such thing. Not a good plan ;-).
  102.  
  103. When you have finished your program, you will want to free the memory
  104. taken up by the virtual screen by doing the following :
  105.  
  106. Procedure ShutDown;
  107. BEGIN
  108.   FreeMem (VirScr,64000);
  109. END;
  110.  
  111. If you don't do this your other programs will have less memory to use for
  112. themselves.
  113.  
  114. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  115. ■  Putting a pixel to your virtual screen
  116.  
  117. This is very similar to putting a pixel to your normal MCGA screen, as
  118. discussed in part one... here is our origonal putpixel :
  119.  
  120. Procedure PutPixel (X,Y : Integer; Col : Byte);
  121. BEGIN
  122.   Mem [VGA:X+(Y*320)]:=col;
  123. END;
  124.  
  125. For our virtual screen, we do the following :
  126.  
  127. Procedure VirtPutPixel (X,Y : Integer; Col : Byte);
  128. BEGIN
  129.   Mem [Vaddr:X+(Y*320)]:=col;
  130. END;
  131.  
  132. It seems quite wasteful to have two procedures doing exactly the same thing,
  133. just to different screens, doesn't it? So why don't we combine the two like
  134. this :
  135.  
  136. Procedure PutPixel (X,Y : Integer; Col : Byte; Where : Word);
  137. BEGIN
  138.   Mem [Where:X+(Y*320)]:=col;
  139. END;
  140.  
  141. To use this, you will say something like :
  142.  
  143. Putpixel (20,20,32,VGA);
  144. PutPixel (30,30,64,Vaddr);
  145.  
  146. These two statements draw two pixels ... one to the VGA screen and one to
  147. the virtual screen! Doesn't that make you jump  with joy! ;-) You will
  148. have noticed that we still can't actually SEE the virtual screen, so on to
  149. the next part ...
  150.  
  151. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  152. ■  How to "Flip" your virtual screen on to the true screen
  153.  
  154. You in fact already have to tools to do this yourselves from information
  155. in the previous parts of this trainer series. We will of course use the
  156. Move command, like so :
  157.  
  158. Move (Virscr^,mem [VGA:0],64000);
  159.  
  160. simple, eh? Yuo may want to wait for a verticle retrace (Part 2) before you
  161. do that, as it may make the flip much smoother (and, alas, slower).
  162.  
  163. Note that most of our other procedures may be altered to support the
  164. virtual screen, such as Cls etc. (see Part 1 of this series), using the
  165. methoods described above (I have altered the CLS procedure in the sample
  166. program given at the end of this Part.)
  167.  
  168. We of ASPHYXIA have used virtual screens in almost all of our demos.
  169. Can you imagine how awful the SoftelDemo would have looked if you had to
  170. watch us redrawing the moving background, text and vectorballs for EACH
  171. FRAME? The flicker, doubling effects etc would have made it awful! So
  172. we used a virtual screen, and are very pleased with the result.
  173. Note, though, that to get the speed we needed to get the demo fast enough,
  174. we wrote our sprites routines, flip routines, pallette routines etc. all
  175. in assembly. The move command is very fast, but not as fast as ASM ;-)
  176.  
  177. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  178. ■  In closing
  179.  
  180. I am writing this on the varsity computers in between lectures. I prefer
  181. writing & coding between 6pm and 4am, but it isn't a good plan when
  182. varsity is on ;-), so this is the first part of the trainer series ever
  183. written before 9pm.
  184.  
  185. I have been asked to do a part on scrolling the screen, so that is
  186. probably what I will do for next week. Also, ASPHYXIA will soon be putting
  187. up a small demo with source on the local boards. It will use routines
  188. that we have discussed in this series, and demonstrate how powerful these
  189. routines can be if used in the correct manner.
  190.  
  191. Some projects for you to do :
  192.   1) Rewrite the flip statement so that you can say :
  193.         flip (Vaddr,VGA);
  194.         flip (VGA,Vaddr);
  195.       ( This is how ASPHYXIAS one works )
  196.  
  197.   2) Put most of the routines (putpixel, cls, pal etc.) into a unit,
  198.      so that you do not need to duplicate the procedures in each program
  199.      you write. If you need help, leave me mail.
  200.  
  201.  
  202. See you next week
  203.    - Denthor
  204.  
  205.  
  206. ┌──────────────┬─────────────────────────────────────────────────────────────
  207. │ TUTPROG4.PAS │
  208. └──────────────┘
  209.  
  210. {$X+}   (* This is a handy little trick to know. If you put this at the top
  211.            of your program, you do not have to set a variable when calling
  212.            a function, i.e. you may just say 'READKEY' instead of
  213.            'CH:=READKEY'                                                *)
  214.  
  215. USES Crt;           (* This has a few nice functions in it, such as the
  216.                        READKEY command.                                 *)
  217.  
  218. CONST VGA = $a000;  (* This sets the constant VGA to the segment of the
  219.                        VGA screen.                                      *)
  220.  
  221. Type Virtual = Array [1..64000] of byte;  { The size of our Virtual Screen }
  222.      VirtPtr = ^Virtual;                  { Pointer to the virtual screen }
  223.  
  224. VAR Virscr : VirtPtr;                      { Our first Virtual screen }
  225.     Vaddr  : word;                        { The segment of our virtual screen}
  226.  
  227.  
  228. {──────────────────────────────────────────────────────────────────────────}
  229. Procedure SetMCGA;  { This procedure gets you into 320x200x256 mode. }
  230. BEGIN
  231.   asm
  232.      mov        ax,0013h
  233.      int        10h
  234.   end;
  235. END;
  236.  
  237.  
  238. {──────────────────────────────────────────────────────────────────────────}
  239. Procedure SetText;  { This procedure returns you to text mode.  }
  240. BEGIN
  241.   asm
  242.      mov        ax,0003h
  243.      int        10h
  244.   end;
  245. END;
  246.  
  247.  
  248. {──────────────────────────────────────────────────────────────────────────}
  249. Procedure Cls (Col : Byte; Where:Word);
  250.    { This clears the screen to the specified color, on the VGA or on the
  251.         virtual screen }
  252. BEGIN
  253.   Fillchar (Mem [where:0],64000,col);
  254. END;
  255.  
  256. {──────────────────────────────────────────────────────────────────────────}
  257. procedure WaitRetrace; assembler;
  258.   { This waits until you are in a Verticle Retrace ... this means that all
  259.     screen manipulation you do only appears on screen in the next verticle
  260.     retrace ... this removes most of the "fuzz" that you see on the screen
  261.     when changing the pallette. It unfortunately slows down your program
  262.     by "synching" your program with your monitor card ... it does mean
  263.     that the program will run at almost the same speed on different
  264.     speeds of computers which have similar monitors. In our SilkyDemo,
  265.     we used a WaitRetrace, and it therefore runs at the same (fairly
  266.     fast) speed when Turbo is on or off. }
  267.  
  268. label
  269.   l1, l2;
  270. asm
  271.     mov dx,3DAh
  272. l1:
  273.     in al,dx
  274.     and al,08h
  275.     jnz l1
  276. l2:
  277.     in al,dx
  278.     and al,08h
  279.     jz  l2
  280. end;
  281.  
  282.  
  283.  
  284.  
  285. {──────────────────────────────────────────────────────────────────────────}
  286. Procedure SetUpVirtual;
  287.    { This sets up the memory needed for the virtual screen }
  288. BEGIN
  289.   GetMem (VirScr,64000);
  290.   vaddr := seg (virscr^);
  291. END;
  292.  
  293.  
  294. {──────────────────────────────────────────────────────────────────────────}
  295. Procedure ShutDown;
  296.    { This frees the memory used by the virtual screen }
  297. BEGIN
  298.   FreeMem (VirScr,64000);
  299. END;
  300.  
  301.  
  302. {──────────────────────────────────────────────────────────────────────────}
  303. Procedure PutPixel (X,Y : Integer; Col : Byte; Where : Word);
  304.    { This puts a pixel at X,Y using color col, on VGA or the Virtual Screen}
  305. BEGIN
  306.   Mem [Where:X+(Y*320)]:=col;
  307. END;
  308.  
  309.  
  310. {──────────────────────────────────────────────────────────────────────────}
  311. Procedure Flip;
  312.    { This flips the virtual screen to the VGA screen. }
  313. BEGIN
  314.   Move (Virscr^,mem [VGA:0],64000);
  315. END;
  316.  
  317. {──────────────────────────────────────────────────────────────────────────}
  318. Procedure BlockMove;
  319.    { This tests various ways of moving a block around the screen }
  320. VAR loop1,loop2,loop3:Integer;
  321. BEGIN
  322.   For loop1:=1 to 50 do BEGIN                     { This draw a block    }
  323.     for loop2:=1 to 50 do                         {  directly to VGA, no }
  324.       for loop3:=1 to 50 do                       {  flipping            }
  325.         putpixel (loop1+loop2,loop3,32,VGA);
  326.     cls (0,VGA);
  327.   END;
  328.  
  329.   For loop1:=1 to 50 do BEGIN                     { This draws a block     }
  330.     for loop2:=1 to 50 do                         { to the virtual screen, }
  331.       for loop3:=1 to 50 do                       { then flips it to VGA   }
  332.         putpixel (loop1+loop2,loop3,32,Vaddr);
  333.     flip;
  334.     cls (0,Vaddr);
  335.   END;
  336.  
  337.   For loop1:=1 to 50 do BEGIN                     { This draws a block     }
  338.     for loop2:=1 to 50 do                         { to the virtual screen, }
  339.       for loop3:=1 to 50 do                       { waits for a retrace,   }
  340.         putpixel (loop1+loop2,loop3,32,Vaddr);    { then flips it to VGA   }
  341.     waitretrace;
  342.     flip;
  343.     cls (0,Vaddr);
  344.   END;
  345. END;
  346.  
  347.  
  348. {──────────────────────────────────────────────────────────────────────────}
  349. Procedure PatternDraw;
  350.    { This test the speed of flipping by drawing two patterns and flipping
  351.      them }
  352. VAR loop1,loop2:integer;
  353. BEGIN
  354.   for loop1:=1 to 100 do                        { This draws pattern one }
  355.     for loop2:=1 to 100 do                      { to the virtual screen  }
  356.       putpixel (loop1,loop2,loop1,Vaddr);       { then flips it to VGA   }
  357.   flip;
  358.  
  359.   for loop1:=1 to 100 do                        { This draws pattern two }
  360.     for loop2:=1 to 100 do                      { to the virtual screen  }
  361.       putpixel (loop1,loop2,loop2,Vaddr);       { then flips it to VGA   }
  362.   flip;
  363. END;
  364.  
  365.  
  366. BEGIN
  367.   ClrScr;
  368.   Writeln ('This program will demonstrate the power of virtual screens.');
  369.   Writeln ('A block will firstly move across the screen, being drawn and');
  370.   Writeln ('erased totally on the VGA. Then the same block will move');
  371.   Writeln ('across, but will be drawn on the virtual screen and flipped');
  372.   Writeln ('to the VGA screen without a retrace (see part 2). The the');
  373.   Writeln ('block will go again, with flipping and a retrace.');
  374.   Writeln;
  375.   Writeln ('I will then draw a pattern, flip it to VGA, draw another');
  376.   Writeln ('pattern, flip it to VGA, and repeat that until a key is pressed.');
  377.   Writeln ('This will demonstrate that even when I put down 10000 pixels,');
  378.   Writeln ('then flip them to the VGA, it is still relatively fast.      ');
  379.   Writeln; Writeln;
  380.   Writeln ('Hit any key to continue ...');
  381.   readkey;
  382.   setmcga;
  383.   setupvirtual;
  384.   cls (0,vaddr);    { After you have got the memory for the virtual screen,
  385.                       it is usually filled with random garbage. It is always
  386.                       wise to clear the virtual screen directly afterwards }
  387.   BlockMove;
  388.  
  389.   Repeat
  390.     PatternDraw;
  391.   Until keypressed;
  392.  
  393.   Readkey;
  394.   settext;
  395.   shutdown;
  396.   Writeln ('All done. This concludes the fourth sample program in the ASPHYXIA');
  397.   Writeln ('Training series. You may reach DENTHOR under the name of GRANT');
  398.   Writeln ('SMITH on the MailBox BBS, or leave a message to ASPHYXIA on the');
  399.   Writeln ('ASPHYXIA BBS. Get the numbers from Roblist, or write to :');
  400.   Writeln ('             Grant Smith');
  401.   Writeln ('             P.O. Box 270');
  402.   Writeln ('             Kloof');
  403.   Writeln ('             3640');
  404.   Writeln ('I hope to hear from you soon!');
  405.   Writeln; Writeln;
  406.   Write   ('Hit any key to exit ...');
  407.   Readkey;
  408. END.
  409.